package org.example.spirngmvcdemo.controller;

import org.example.spirngmvcdemo.model.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

// 用来返回数据
//@RestController
// 用来返回静态页面
@Controller
// 返回数据
//@ResponseBody
@RequestMapping("/response")
public class ResponseController {

    // 返回 静态页面
    @RequestMapping("/r1")
    public String r1() {
        return "/index.html";
    }

    // @ResponseBody 返回数据
    @ResponseBody
    @RequestMapping("/r2")
    public String r2() {
        return "/index.html";
    }

    // 返回 HTML片段 (postman 返回 纯文本   浏览器返回HTML)
    @ResponseBody
    @RequestMapping("/r3")
    public String r3() {
        return "<h1>我是HTML片段</h1>";
    }

    // postman 返回纯文本  浏览器返回 HTML
    @ResponseBody
    @RequestMapping("/r4")
    public String r4() {
        return "{\"OK\": 1}";
    }


    // 返回 js
    @RequestMapping("/r5")
    public String r5() {
        return "/a.js";
    }

    // 返回 css
    @RequestMapping("/r6")
    public String r6() {
        return "/b.css";
    }

    // 返回 对象时，Spring会自动将类型(Content-Type)设置为 json
    @ResponseBody
    @RequestMapping("/r7")
    public UserInfo r7() {
        UserInfo userInfo = new UserInfo(1,"lisi",18);
        return userInfo;
    }

    // 返回 json
    @ResponseBody
    @RequestMapping("/r8")
    public Map<String, String> r8() {
        HashMap<String, String> map = new HashMap<>();
        map.put("OK", "OK");
        map.put("No", "No");
        return map;
    }

    // 可以手动设置 返回类型(Content-Type)
    @ResponseBody
    @RequestMapping(value = "/r9", produces = "application/json; charset=utf-8")
    public String r9() {
        return "{\"OK\": 1}";
    }

    // 自定义 Header
    @ResponseBody
    @RequestMapping("/r10")
    public String r10(HttpServletResponse response) {
        response.setHeader("myHeader", "myHeaderValue");
        return "设置 Header 成功";
    }

    @ResponseBody
    @RequestMapping("/r11")
    public String r11(HttpServletResponse response) {
        response.setStatus(401);
        return "设置状态码成功";
    }


}
